package cn.wolfcode.car.business.web.controller;

import cn.wolfcode.car.base.domain.User;
import cn.wolfcode.car.base.service.IUserService;
import cn.wolfcode.car.business.domain.Statement;
import cn.wolfcode.car.business.domain.StatementItem;
import cn.wolfcode.car.business.query.StatementItemQuery;
import cn.wolfcode.car.business.service.IStatementItemService;
import cn.wolfcode.car.business.service.IStatementService;
import cn.wolfcode.car.common.base.page.TablePageInfo;
import cn.wolfcode.car.common.web.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("business/statementItem")
public class StatementItemController {
    //模板前缀
    private static final String prefix = "business/statementItem/";

    @Autowired
    private IStatementService statementService;

    @Autowired
    private IStatementItemService statementItemService;

    @Autowired
    private IUserService userService;

    @RequestMapping("/itemDetail")
    public String itemDetail(Model model,Long statementId) {
        Statement statement = statementService.get(statementId);
        model.addAttribute("statement",statement);

        User user = userService.get(statement.getPayeeId());
        model.addAttribute("user",user);

        // 结算单状态决定跳转的页面
        if (Statement.STATUS_CONSUME.equals(statement.getStatus())) {
            return prefix + "consumeDetail";
        }else {
            return prefix + "payDetail";
        }
    }

    @RequestMapping("/query")
    @ResponseBody
    public TablePageInfo<StatementItem> query(Long statementId,StatementItemQuery qo) {
        return statementItemService.query(statementId,qo);
    }

    @RequestMapping("/saveItems")
    @ResponseBody
    public AjaxResult saveItems(Long statementId,@RequestBody StatementItemQuery qo) {
        statementItemService.saveItems(statementId,qo);
        return AjaxResult.success();
    }
}
